<?php

    /**
     * @package      Modules
     * @subpackage   Base_Classes_Customs
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Base_Classes_Customs_EditPassword implements Base_Classes_Customs_Interface
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Prepares form values.
         *
         * @param       DbBase     $element
         * @param       array       $params
         * @return      array
         */
        public static function prepare(DbBase $element, array $params = array())
        {
            return array();
        }

        /**
         * Processes the element
         *
         * Throws a Base_Classes_Customs_Exception if the element is invalid (global validation).
         *
         * @throws      Base_Classes_Customs_Exception
         * @param       &DbBase     $element
         * @param       array       $params
         */
        public function process(DbBase &$element, array $params = array())
        {
            $oldpassword        =& $params['custom-'. __CLASS__ .'-oldpassword'];
            $newpassword        =& $params['custom-'. __CLASS__ .'-newpassword'];
            $repeatnewpassword  =& $params['custom-'. __CLASS__ .'-repeatnewpassword'];

            if (!empty($newpassword)) {
                // Checks if old password corresponds to real former password
                if (sha1($oldpassword) != $element->password) {
                    throw new Base_Classes_Customs_Exception(array('custom-'. __CLASS__ .'-oldpassword' => array('oldpassword')));
                }

                // Checks if new password and repetition are the same
                if ($newpassword != $repeatnewpassword) {
                    throw new Base_Classes_Customs_Exception(array('custom-'. __CLASS__ .'-repeatnewpassword' => array('passwordnotequal')));
                }

                $element->password = sha1(stripslashes($newpassword));
            }
        }

        /**
         * Validates elements fields
         * 
         * Throws a Base_Classes_Customs_Exception if field is invalid.
         *
         * @throws      Base_Classes_Customs_Exception
         * @param       DbBase     $element
         * @param       string      $fieldname
         * @param       mixed       $value
         */
        public function validate(DbBase $element, $fieldname, $value)
        {

        }
    }